import { Folder } from '~/types/file'
import { PayloadAction, createSlice } from '@reduxjs/toolkit'
export interface Breadcrumbs {
  id: number
  name: string
}
interface FileState {
  folder: Folder
  totalSize: number
  breadcrumbs: Breadcrumbs[]
}

const initialState: FileState = {
  // 默认的文件路径以及文件类型
  folder: {
    userId: 0,
    typeId: 0,
    folderId: 0
  },
  totalSize: 0,
  breadcrumbs: [
    {
      id: 0,
      name: '首页'
    }
  ]
}

const fileSlice = createSlice({
  name: 'file',
  initialState,
  reducers: {
    setFileInfo(state, action: PayloadAction<Folder>) {
      state.folder = action.payload
    },
    setTotalSize(state, action: PayloadAction<number>) {
      state.totalSize = action.payload
    },
    setBreadcrumbs(state, action: PayloadAction<Breadcrumbs[]>) {
      state.breadcrumbs = action.payload
      state.folder.folderId = action.payload[action.payload.length - 1].id
    }
  }
})

export const { setFileInfo, setTotalSize, setBreadcrumbs } = fileSlice.actions

export default fileSlice.reducer
